package com.richapp.launcher.database.system.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.stmt.QueryBuilder;
import com.richapp.launcher.database.infrared.dao.BrandDao;
import com.richapp.launcher.database.infrared.dao.BrandModelDao;
import com.richapp.launcher.database.system.SystemBaseDao;
import com.richapp.launcher.database.infrared.mode.Brand;
import com.richapp.launcher.database.infrared.mode.BrandModel;
import com.richapp.launcher.database.system.mode.UserMachine;
import com.richapp.launcher.database.system.mode.UserMachineButton;

import java.util.List;

public class UserMachineDao extends SystemBaseDao<UserMachine>{

    private BrandDao brandDao = null;
    private Context context;

	public UserMachineDao(Context context) {

        super(context,UserMachine.class);
        this.context = context;
        brandDao = new BrandDao(context);
	}

    public List<UserMachine> getList(String userName){
        List<UserMachine> list = null;
        try {
            QueryBuilder queryBuilder = gettOp().queryBuilder();
            queryBuilder.where().eq("userName",userName);
            list = queryBuilder.query();
        }catch (Exception e){
            Log.e(getClass().getName(), e.getMessage());
        }
        return list;
    }

    public List<UserMachine> getListDetail(String userName){
        List<UserMachine> list = null;
        try {
            QueryBuilder queryBuilder = gettOp().queryBuilder();
            queryBuilder.where().eq("userName",userName);
            list = queryBuilder.query();
            if(list != null){
                UserMachineButtonDao userMachineButtonDao = new UserMachineButtonDao(context);
                BrandModelDao brandModelDao = new BrandModelDao(context);
                List<UserMachineButton> buttonList = null;
                Brand brand  =  null;
                BrandModel brandModel;
                for(UserMachine um : list){
                    buttonList = userMachineButtonDao.getList(um.getMachineName());
                    um.setButtonList(buttonList);
                    brandModel = brandModelDao.get(um.getBrandModelId());
                    if(brandModel != null){
                        brand = brandDao.get(brandModel.getBrandId());
                        if(brand != null){
                            um.setMachineTypeId(brandModel.getMachineTypeId());
                            um.setModelName(brandModel.getModelName());
                            um.setBrandName(brand.getBrandCh());
                        }
                    }

                }
            }
        }catch (Exception e){
            Log.e(getClass().getName(), e.getMessage());
        }
        return list;
    }

    public UserMachine get(String userName,String machineName){
        UserMachine userMachine = null;
        try {
            QueryBuilder queryBuilder = gettOp().queryBuilder();
            queryBuilder.where().eq("userName",userName).and().eq("machineName",machineName);
            Object obj = queryBuilder.queryForFirst();
            if(obj instanceof UserMachine){
                userMachine = (UserMachine)obj;
            }
        }catch (Exception e){
            Log.e(getClass().getName(), e.getMessage());
        }
        return userMachine;
    }

    public UserMachine get(String bindBoxCpuId,String bindDeviceId,String bindDeviceNumber,String bindWay){
        UserMachine userMachine = null;
        try {
            QueryBuilder queryBuilder = gettOp().queryBuilder();
            queryBuilder.where().eq("bindBoxCpuId",bindBoxCpuId)
                    .and().eq("bindDeviceId",bindDeviceId)
                    .and().eq("bindDeviceNumber",bindDeviceNumber)
                    .and().eq("bindWay",bindWay);
            Object obj = queryBuilder.queryForFirst();
            if(obj instanceof UserMachine){
                userMachine = (UserMachine)obj;
            }
        }catch (Exception e){
            Log.e(getClass().getName(), e.getMessage());
        }
        return userMachine;
    }
}
